﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using XRpgLibrary;
using XRpgLibrary.Controls;
using XRpgLibrary.SpriteClasses;
using XRpgLibrary.TileEngine;
using XRpgLibrary.WorldClasses;
using XRpgLibrary.CharacterClasses;

using RpgLibrary.CharacterClasses;
using RpgLibrary.WorldClasses;

using SourceOfMagic.Components;

namespace SourceOfMagic.GameScreens
{
    public class LoadGameScreen : BaseGameState
    {
        #region Field Region

        PictureBox backgroundImage;
        ListBox loadListBox;
        LinkLabel loadLinkLabel;
        LinkLabel exitLinkLabel;
        
        #endregion
        
        #region Property Region
        #endregion
        
        #region Constructor Region

        public LoadGameScreen(RpgGame game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion
        
        #region Method Region
        
        protected override void LoadContent()
        {
            base.LoadContent();
            
            ContentManager Content = Game.Content;
            
            backgroundImage = new PictureBox(
                Content.Load<Texture2D>(@"Backgrounds\title-screen-temp"),
                GameRef.ScreenRectangle);
            ControlManager.Add(backgroundImage);

            loadLinkLabel = new LinkLabel();
            loadLinkLabel.Text = "Select game";
            loadLinkLabel.Position = new Vector2(50, 100);
            loadLinkLabel.Selected += new EventHandler(loadLinkLabel_Selected);
            ControlManager.Add(loadLinkLabel);

            exitLinkLabel = new LinkLabel();
            exitLinkLabel.Text = "Back";
            exitLinkLabel.Position = new Vector2(50, 100 + exitLinkLabel.SpriteFont.LineSpacing);
            exitLinkLabel.Selected += new EventHandler(exitLinkLabel_Selected);
            ControlManager.Add(exitLinkLabel);

            loadListBox = new ListBox(
                Content.Load<Texture2D>(@"GUI\listBoxImage"),
                Content.Load<Texture2D>(@"GUI\rightarrowUp"));
            loadListBox.Position = new Vector2(400, 100);
            loadListBox.Selected += new EventHandler(loadListBox_Selected);
            loadListBox.Leave += new EventHandler(loadListBox_Leave);
            for (int i = 0; i < 20; i++)
                loadListBox.Items.Add(String.Format("Game number: {0}", i));
            ControlManager.Add(loadListBox);
            
            ControlManager.NextControl();
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.spriteBatch.Begin();

            base.Draw(gameTime);
            ControlManager.Draw(GameRef.spriteBatch);
            
            GameRef.spriteBatch.End();
        }

        #endregion
        
        #region Method Region
        
        void loadListBox_Leave(object sender, EventArgs e)
        {
            ControlManager.AcceptInput = true;
        }
        
        void loadLinkLabel_Selected(object sender, EventArgs e)
        {
            ControlManager.AcceptInput = false;
            loadLinkLabel.HasFocus = false;
            loadListBox.HasFocus = true;
        }
        
        void loadListBox_Selected(object sender, EventArgs e)
        {
            loadLinkLabel.HasFocus = true;
            loadListBox.HasFocus = false;
            ControlManager.AcceptInput = true;

            TransitionChange(GameRef.GamePlayScreen);
            
            CreatePlayer();
            CreateWorld();
            Character.ScriptContext = scriptContext;
        }

        void exitLinkLabel_Selected(object sender, EventArgs e)
        {
            TransitionPop();
        }
        
        private void CreatePlayer()
        {
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey,Animation>();
            
            Animation animation = new Animation(12, 64, 160, 0, 0);
            animation.FramesPerSecond = 60;
            animations.Add(AnimationKey.South, animation);

            animation = new Animation(12, 64, 160, 0, 160);
            animation.FramesPerSecond = 60;
            animations.Add(AnimationKey.West, animation);

            animation = new Animation(12, 64, 160, 0, 320);
            animation.FramesPerSecond = 60;
            animations.Add(AnimationKey.East, animation);

            animation = new Animation(12, 64, 160, 0, 480);
            animation.FramesPerSecond = 60;
            animations.Add(AnimationKey.North, animation);
            
            AnimatedSprite sprite = new AnimatedSprite(
                GameRef.Content.Load<Texture2D>(@"PlayerSprites\K_Walking_Test"),
                animations, 
                GameRef.spriteBatch);

            sprite.TextureImageName = @"PlayerSprites\K_Walking_Test";

            CharacterData data = new CharacterData();
            data.Entity = new EntityData("Fighter", 1, 1, 1, 1, 1, 1, "", "", "");
            data.PassThrough = false;
            data.RespectPassability = true;

            data.Sprite = sprite.GetDataObject();

            Character character = new Character(
                data,
                sprite,
                GameRef.Content, 
                GameRef.spriteBatch, 
                scriptContext);

            character.PassThrough = true;
            
            GamePlayScreen.Player = character;
            scriptContext.Player = GamePlayScreen.Player;
        }

        private void CreateWorld()
        {
            World world = new World(
                GameRef, 
                GameRef.ScreenRectangle, 
                GameRef.spriteBatch, 
                scriptContext);
            
            world.Levels[0].Map.InsertPlayer(1, GamePlayScreen.Player);

            world.CurrentLevelIndex = 0;

            GamePlayScreen.World = world;
            scriptContext.World = GamePlayScreen.World;
        }

        #endregion
    }
}